package com.spider.task;

import java.io.IOException;

import main.Main;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.db.mongodb.MMongoDB;
import com.httpclient.SpiderHttpClientManager;
import com.spider.bean.ZhilianJobBean;
import com.spider.util.SpiderCondition;
import com.spider.util.SpiderProperties;

public class SpiderTask implements Runnable {

	private static final Logger log = Logger.getLogger(SpiderTask.class);

	public void run() {
		getJobURL();
	}

	/**
	 * 获得工作详情页的 url
	 */
	public void getJobURL() {
		String content = getContentByUrl(getContentUrl());
		Document doc = Jsoup.parse(content);
		Elements eles = doc.select(".showTR");
		// 获得url
		Elements tmp = null;
		ZhilianJobBean bean = null;
		String job = "";
		String comp = "";
		String jobName="";
		for (Element el : eles) {
			tmp = el.getElementsByTag("a");
			bean = new ZhilianJobBean();
			job = tmp.get(0).getElementsByTag("a").attr("href");
			jobName = tmp.get(0).getElementsByTag("a").text();
			// 获得公司名称
			comp = tmp.get(1).getElementsByTag("a").text();
//			System.out.println(Thread.currentThread().getName()+"-----------------------" + job + "          "
//					+jobName+"             "+ comp);
			bean.setJobUrl(job);
			bean.setCompanyName(comp);
			bean.setJobName(jobName);
//			if (!SpiderBlockQueue.jobBeanQueue.offer(bean)) {
//				if (!SpiderBlockQueue.jobBeanQueue2.offer(bean)) {
//					log.error("put  object to jobbeanQueue false!!!!!");
//					//将数据保存到 mongodb
//					MMongoDB db = new MMongoDB();
//					db.save(SpiderCondition.JOB_BEAN_TABLE_NAME, bean);
//				}
//			}
			//存入mongodb
			MMongoDB db = new MMongoDB();
			db.save(SpiderCondition.JOB_BEAN_TABLE_NAME, bean);
		}
	}

	/**
	 * 通过url 获得 内容
	 * 
	 * @param url
	 */
	public String getContentByUrl(String url) {
		HttpClient http = SpiderHttpClientManager.getInstance();
		GetMethod get = new GetMethod(url);
		String result = "";
		try {
			get.setRequestHeader("Connection","keep-alive");
			get.setRequestHeader("Cookie","LastCity%5Fid=530; LastCity=%e5%8c%97%e4%ba%ac; dywez=95841923.1363184649.1.1.dywecsr=(direct)|dyweccn=(direct)|dywecmd=(none); __zpWAM=1363184690831.209630.1363184691.1363184691.1; dywea=95841923.1486548030822642000.1363184649.1363184649.1363184649.1; dywem=95841923.y; __utma=269921210.867832535.1363184648.1363184648.1363184648.1; __utmz=269921210.1363184648.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=269921210.|2=Member=395599436=1");
			get.setRequestHeader("Host","keep-alive");
			get.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");  
			get.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
			get.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");   
			get.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); 
			get.setRequestHeader("Connection", "keep-alive"); 
			
			get.getParams().setParameter("jl", "530");
			get.getParams().setParameter("kw", "java");
			get.getParams().setParameter("sm", "0");
			get.getParams().setParameter("p", "1");
			get.getParams().setParameter("sf", "0");
			
			int code = http.executeMethod(get);
			if (code == HttpStatus.SC_OK) {
				byte[] r = get.getResponseBody();
				result = new String(r);
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 释放链接
			get.releaseConnection();
		}
		return result;
	}

	private String getContentUrl() {
		synchronized (Main.page) {
			String baseUrl = "http://sou.zhaopin.com/Jobs/searchresult.ashx?jl="
					+ SpiderProperties
							.getString(SpiderCondition.QUERY_JOB_ADDRESS)
					+ "&kw="
					+ SpiderProperties
							.getString(SpiderCondition.QUERY_KEY_WORD)
					+ "&sm=0&p=" + getPage() + "&sf=0";
			addPage();
			return baseUrl;
		}
	}

	public static Integer getPage() {
		synchronized (Main.page) {
			return Main.page;
		}
	}
	public static void addPage() {
		synchronized (Main.page) {
			Main.page++;
		}
	}

}
